package aip.assignment2.persistence.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Stone
 */
@Entity
@Table(name = "purchase")
@XmlRootElement
@NamedQueries(
{
	@NamedQuery(name = "Purchase.findAll", query = "SELECT p FROM Purchase p"),
	@NamedQuery(name = "Purchase.findAllOrderByNumber", query = "SELECT p FROM Purchase p ORDER BY p.number DESC"),
	@NamedQuery(name = "Purchase.findById", query = "SELECT p FROM Purchase p WHERE p.id = :id"),
	@NamedQuery(name = "Purchase.findByNumber", query = "SELECT p FROM Purchase p WHERE p.number = :number"),
	@NamedQuery(name = "Purchase.findByStatus", query = "SELECT p FROM Purchase p WHERE p.status = :status"),
	@NamedQuery(name = "Purchase.findOrderedOrPaidPurchases", query = "SELECT p FROM Purchase p WHERE p.status LIKE 'ORDERED' OR p.status LIKE 'PAID'"),
	@NamedQuery(name = "Purchase.findOrderedOrPaidPurchasesByNumber", query = "SELECT p FROM Purchase p WHERE (p.number LIKE :number) AND (p.status LIKE 'ORDERED' OR p.status LIKE 'PAID')"),
	@NamedQuery(name = "Purchase.findByCreatedOn", query = "SELECT p FROM Purchase p WHERE p.createdOn = :createdOn"),
	@NamedQuery(name = "Purchase.findBySurname", query = "SELECT p FROM Purchase p WHERE p.customer.name.surname = :surname"),
	@NamedQuery(name = "Purchase.findByNumberAndSurname", query = "SELECT p FROM Purchase p WHERE p.number = :number AND p.customer.name.surname = :surname"),
})
public class Purchase implements Serializable
{
	private static final long serialVersionUID = 1L;
	@Id
	@SequenceGenerator(name = "purchase_id", allocationSize = 1, sequenceName = "purchase_id_seq")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "purchase_id")
	@Basic(optional = false)
	@Column(name = "id")
	private Integer id;
	@Basic(optional = false)
	@NotNull
	@Size(min = 1, max = 100)
	@Column(name = "number")
	private String number;
	@Basic(optional = false)
	@NotNull
	@Size(min = 1, max = 7)
	@Column(name = "status")
	private String status;
	@Basic(optional = false)
	@NotNull
	@Column(name = "created_on")
	@Temporal(TemporalType.TIMESTAMP)
	private Date createdOn;
	@JoinColumn(name = "customer", referencedColumnName = "id")
	@ManyToOne(optional = false)
	private Customer customer;
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "purchase")
	private List<PurchaseProduct> purchaseProductList;
	public Purchase()
	{
	}
	public Purchase(Integer id)
	{
		this.id = id;
	}
	public Purchase(Integer id, String number, String status, Date createdOn)
	{
		this.id = id;
		this.number = number;
		this.status = status;
		this.createdOn = createdOn;
	}
	public Integer getId()
	{
		return id;
	}
	public void setId(Integer id)
	{
		this.id = id;
	}
	public String getNumber()
	{
		return number;
	}
	public void setNumber(String number)
	{
		this.number = number;
	}
	public String getStatus()
	{
		return status;
	}
	public void setStatus(String status)
	{
		this.status = status;
	}
	public Date getCreatedOn()
	{
		return createdOn;
	}
	public void setCreatedOn(Date createdOn)
	{
		this.createdOn = createdOn;
	}
	public Customer getCustomer()
	{
		return customer;
	}
	public void setCustomer(Customer customer)
	{
		this.customer = customer;
	}
	@XmlTransient
	public List<PurchaseProduct> getProducts()
	{
		return purchaseProductList;
	}
	public void setPurchaseProductList(List<PurchaseProduct> purchaseProductList)
	{
		this.purchaseProductList = purchaseProductList;
	}
	public BigDecimal getTotal()
	{
		BigDecimal total = new BigDecimal(BigInteger.ZERO);
		BigDecimal currentTotal = new BigDecimal(BigInteger.ZERO);
		for (PurchaseProduct p : purchaseProductList)
		{
			currentTotal = p.getProduct().getPrice().multiply(new BigDecimal(p.getQuantity()));
			total = total.add(currentTotal);
		}
		return total;
	}
	@Override
	public int hashCode()
	{
		int hash = 0;
		hash += (id != null ? id.hashCode() : 0);
		return hash;
	}
	@Override
	public boolean equals(Object object)
	{
		// TODO: Warning - this method won't work in the case the id fields are not set
		if (!(object instanceof Purchase))
		{
			return false;
		}
		Purchase other = (Purchase) object;
		if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
		{
			return false;
		}
		return true;
	}
	@Override
	public String toString()
	{
		return "aip.assignment2.data.entity.Purchase[ id=" + id + " ]";
	}
}
